/**
 * Created by shawn on 17/9/9.
 */
var id = 0;

function nextId() {
    id++;
    return id;
}

function Stock(name,price,rating,desc,categories) {
    this.id = nextId();
    this.name = name;
    this.price = price;
    this.rating = rating;
    this.desc = desc;
    this.categories = categories;
};

var stocks = [
    new Stock("第一只股票", 1.99, 3.5, "只是第一只股票", ["IT", "互联网"]),
    new Stock("第二只股票", 2.99, 1.5, "只是第一只股票", ["IT"]),
    new Stock("第三只股票", 3.99, 3.5, "只是第一只股票", ["金融"]),
    new Stock("第四只股票", 4.99, 4.5, "只是第一只股票", ["IT", "互联网"])
];

var getStocks = () =>{
    return stocks;
}

var getStock = (id) =>{
   // var stock = getStocks.find(stock=>stock.id === id);
   // if(!stock){
   //     return new Stock(1, "", 0, 0, "", []);
   // }
   // return stock;
    var i;
    for (i = 0; i <stocks.length; i++) {
        if (stocks[i].id==id){
            return stocks[i];
        }
    }
    return new Stock(1, "", 0, 0, "", []);
}

var createStock = (name,price,rating,desc,categories) =>{
    var stock = new Stock(name,price,rating,desc,categories)
    stocks.push(stock);
    return stock;
}

var deleteStock = (id)=>{
    var index = -1;
    i;

    for(i=0;i<stocks.length;i++){
        if(stocks[i].id === id){
            index = i;
            break;
        }
    }

    if(index >= 0){
        return stocks.splice(index,1)[0];
    }

    return null;
}

module.exports = {
    getStocks:getStocks,
    getStock:getStock,
    createStock:createStock,
    deleteStock:deleteStock
}




